package com.product.system.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.jwt.JWT;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.product.system.domain.SysDictType;
import com.product.system.service.DictTypeService;
import com.product.system.utils.Result;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("dictType")
@RefreshScope
public class SysDictTypeController {
//    @Autowired
//    private MinioClient minioClient;


    @Autowired
    DictTypeService dictTypeService;

    @Autowired
    HttpServletRequest request;

//    @PostMapping("/upload")
//    public Map<String,Object> upload(MultipartFile file)  {
//        Map<String,Object> map = new HashMap<String,Object>();
//        String fileName = UUID.randomUUID()+"_"+file.getOriginalFilename();
//        try {
//            minioClient.putObject(PutObjectArgs
//                    .builder()
//                    .object(fileName)
//                    .bucket("my-test01")
//                    .stream(file.getInputStream(), file.getSize(), -1)
//                    .contentType(file.getContentType())
//                    .build());
//            map.put("code",1001);
//            map.put("url","http://localhost:9000/my-test01/"+fileName);
//        }catch (Exception ex){
//            ex.printStackTrace();
//            map.put("code",1002);
//        }
//        return map;
//    }
    @RequestMapping("/list")
    public Result list( Integer pageNum, Integer pageSize,String dictType) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysDictType> list = dictTypeService.dictTypeList(dictType);
        PageInfo<SysDictType> page = new PageInfo<>(list);
        return Result.success(page);
    }

    @RequestMapping("/getDictTypeInfo")
    public Result getDictTypeInfo() {
        List<SysDictType> typeInfo = dictTypeService.getTypeInfo();
        return Result.success(typeInfo);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody SysDictType sysDictType) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        sysDictType.setCreateTime(format);
        boolean b = dictTypeService.dictTypeAdd(sysDictType);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("/del")
    public Result del(Integer[] dictTypeIdList) {
        boolean b = dictTypeService.dictTypeDel(dictTypeIdList);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("/selectById")
    public Result selectById(Integer id) {
        SysDictType sysDictType = dictTypeService.getById(id);
        return Result.success(sysDictType);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody SysDictType sysDictType) {
        String token = request.getHeader("token");
        String username = (String) JWT.of(token).getPayload("userName");
        sysDictType.setUpdateBy(username);
        Date date = new Date();
        sysDictType.setUpdateTime(date);
        boolean b = dictTypeService.dictTypeUpdate(sysDictType);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws Exception {
        List<SysDictType> list = dictTypeService.List();
        String uuid = IdUtil.getSnowflake().nextIdStr();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("字典"+uuid, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SysDictType.class).sheet("岗位数据").doWrite(list);
    }
}
